<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/5
 * Time: 9:25
 */

namespace app\alliance\controller;


use app\service\MemberService;
use app\service\ResourceService;
use think\Db;

class Message extends Common
{
    /**
     * @return array
     * @content 查询未查看的系统消息 , 团队消息数量
     */
    function messageNum()
    {
        // 未查看的系统消息数量
        $systemMessage = Db::table('notice_news nn')
            ->leftJoin('notice n', 'n.id=nn.notice_id')
            ->where(array('type' => 1, 'member_code' => $this->code, 'status' => 1, 'read_status' => 1))
            ->where("send_time <= '" . date('Y-m-d H:i:s') . "'")
            ->count();
        // 未查看的团队消息数量
        $teamNews = Db::table('notice_news nn')
            ->leftJoin('notice n', 'n.id=nn.notice_id')
            ->where(array('type' => 4, 'member_code' => $this->code, 'status' => 1, 'read_status' => 1))
            ->where("send_time <= '" . date('Y-m-d H:i:s') . "'")
            ->count();
        // 好友申请
        $friends = Db::table('friends')->where(array('friends_code' => $this->code, 'status' => 1))->count();
        return array('status' => true, 'msg' => '查询消息数量', 'systemNum' => $systemMessage, 'teamNum' => $teamNews, 'friendsNum' => $friends);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 系统消息/团队消息
     */
    function systemMessage()
    {
        $type = input('post.type');
        if (empty($type)) {
            # 默认系统消息
            $type = 1;
        }
        # 查询系统消息
        $info = Db::table('notice_news nn')
            ->field('n.id,title,content,explain,send_time,read_status')
            ->leftJoin('notice n', 'n.id=nn.notice_id')
            ->where(array('type' => $type, 'member_code' => $this->code, 'status' => 1))
            ->where("send_time <= '" . date('Y-m-d H:i:s') . "'")
            ->order("id desc")
            ->select();
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**\
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 消息详情(修改查看状态/增加查看人数)
     */
    function systemDetail()
    {
        $id = input('post.id');
        if (!empty($id)) {
            # 查询消息详情
            $detail = Db::table('notice')->where(array('id' => $id))->find();
            # 改为已读
            Db::table('notice_news')
                ->where(array('member_code' => $this->code, 'notice_id' => $id))
                ->update(array('read_status' => 2, 'read_time' => date('Y-m-d H:i:s')));
            # 增加已读人数
            Db::table('notice')->where(array('id' => $id))->setInc('read_num', 1);
            $detail['content'] = ResourceService::ContentStaticReplace($detail['content']);
            return array('status' => true, 'msg' => '查询成功', 'detail' => $detail);
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 发送团队消息
     */
    function sendTeamNews()
    {
        $title = input('post.title');
        $content = input('post.content');
        $people = input('post.people');
        if (!empty($people) and !empty($content) and !empty($title)) {
            # 查询人群
            $peopleReturn = MemberService::relationship($this->code, $this->memberInfo['team_id'], $people);
            if ($peopleReturn['status'] == false) {
                return array('status' => false, 'msg' => '接收人群为空');
            }
            $peopleInfo = $peopleReturn['peopleInfo'];
            # 添加消息表
            $noticeId = Db::table('notice')->insertGetId(array(
                'title' => $title,
                'content' => $content,
                'send_time' => date('Y-m-d H:i:s'),
                'send_name' => $this->memberInfo['name'],
                'status' => 1,
                'type' => 4,
                'create_time' => date('Y-m-d H:i:s')
            ));
            # 消息发送
            $_arr = array();
            foreach ($peopleInfo as $k => $v) {
                array_push($_arr, array('notice_id' => $noticeId, 'member_code' => $v, 'read_status' => 1));
            }
            Db::table('notice_news')->insertAll($_arr);
            return array('status' => true, 'msg' => '发送成功');
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }
}